package cn.com.main;

import cn.com.utils.DatabaseUtil;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 搜索数据库字段 存在的表 并打印 一条值 脚本
 *
 * @author LiuGuodong
 * @date 2024/03/04
 */
public class FileSearchScript {


    public static void main(String[] args) {
        List<String> matchedTables = new ArrayList<>();
        Map<String, Map<String, String>> hashMap = new HashMap<>();

        try (Connection connection = DatabaseUtil.getConnection()) {
            DatabaseMetaData metaData = connection.getMetaData();
            ResultSet resultSet = metaData.getTables(null, null, "%", new String[]{"TABLE"});

            while (resultSet.next()) {
                String tableName = resultSet.getString("TABLE_NAME");
                String sql = "SELECT * FROM " + tableName + " LIMIT 1 offset 0";

                try (PreparedStatement preparedStatement = connection.prepareStatement(sql);
                     ResultSet resultSet1 = preparedStatement.executeQuery()) {

                    ResultSetMetaData rsMetaData = resultSet1.getMetaData();
                    int columnCount = rsMetaData.getColumnCount();

                    while (resultSet1.next()) {
                        Map<String, String> map = new HashMap<>();
                        for (int i = 1; i <= columnCount; i++) {
                            String columnName = rsMetaData.getColumnName(i);
                            String value = resultSet1.getString(columnName);
                            if (isPossiblyEncrypted(value)) {
                                map.put(columnName, value);
                            }
                        }
                        if (!map.isEmpty()) {
                            hashMap.put(tableName, map);
                        }
                    }
                } catch (Exception e) {
                }
            }

            // 输出匹配的表和列值
            System.out.println("匹配的表和列值：");
            System.out.println("--------------------");

            hashMap.forEach((tableName, columnMap) -> {
                System.out.println("表名：" + tableName);
                System.out.println("匹配的列：");

                columnMap.forEach((columnName, columnValue) -> {
                    matchedTables.add(tableName + "\t" + columnName + "\t" + columnValue);
                    System.out.println("\t" + columnName + ": " + columnValue);
                });

                System.out.println();
            });

            System.out.println("--------------------");
            Files.write(Paths.get("E:\\cloud_star\\out.txt"), matchedTables);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 判断字符串是否可能是加密后的结果
    public static boolean isPossiblyEncrypted(String input) {
        if (Objects.nonNull(input) && !input.isEmpty() && "DCS-TYPE".contains(input)) {
            return true;
        }
        return false;
        //if (StringUtil.isBlank(input) || input.length() > 1024) {
        //    return false;
        //}
        //return input.contains("=") || input.contains("CIPHER") || input.contains("--");
    }


}
